package com.wr.elasticsearch.controller;

import com.alibaba.fastjson.JSON;
import com.wr.elasticsearch.entity.Content;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.jsoup.Jsoup;
import org.jsoup.internal.StringUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

@RestController
public class GetGoodsListController {

    @Autowired
    private RestHighLevelClient restHighLevelClient;


    @GetMapping("/getGoods/{keyword}")
    public Boolean getGoods(@PathVariable("keyword") String keyword) throws IOException {

        String url = "https://search.jd.com/Search?keyword="+keyword;

        // 解析网页，获取document树
        Document document = Jsoup.parse(new URL(url), 30000);
        // js中的方法
        Element j_goodsList = document.getElementById("J_goodsList");

        Elements li = j_goodsList.getElementsByTag("li");
        System.out.println(li);
        ArrayList<Content> contents = new ArrayList<>();

        // 新建批处理
        BulkRequest bulkRequest = new BulkRequest();
        System.out.println(li.size());
        for (Element element : li) {
            String img = element.getElementsByTag("img").eq(0).attr("data-lazy-img");
            String price = element.getElementsByClass("p-price").eq(0).text();
            String name = element.getElementsByClass("p-name").eq(0).text();
            System.out.println(img);
            System.out.println(price);
            System.out.println(name);
            System.out.println("-------------------------------");

            Content content = new Content(name,price,img);
            contents.add(content);
            bulkRequest.add(new IndexRequest("jd-goodslist").source(JSON.toJSONString(content), XContentType.JSON));
        }
        BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        return !bulk.hasFailures();
    }
}
